package com.mall.service.marketing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.commons.common.CommonPage;
import com.mall.model.domain.SmsCoupon;
import com.mall.model.domain.SmsCouponProductCategoryRelation;
import com.mall.model.domain.SmsCouponProductRelation;
import com.mall.model.dto.SmsCouponDto;
import com.mall.service.marketing.mapper.SmsCouponMapper;
import com.mall.service.marketing.mapper.SmsCouponProductCategoryRelationMapper;
import com.mall.service.marketing.mapper.SmsCouponProductRelationMapper;
import com.mall.service.marketing.service.SmsCouponProductCategoryRelationService;
import com.mall.service.marketing.service.SmsCouponProductRelationService;
import com.mall.service.marketing.service.SmsCouponService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SmsCouponServiceImpl extends ServiceImpl<SmsCouponMapper, SmsCoupon>
        implements SmsCouponService {
    @Autowired
    private SmsCouponMapper smsCouponMapper;
    @Autowired
    private SmsCouponProductRelationMapper smsCouponProductRelationMapper;
    @Autowired
    private SmsCouponProductCategoryRelationMapper smsCouponProductCategoryRelationMapper;
    @Autowired
    private SmsCouponProductRelationService smsCouponProductRelationService;
    @Autowired
    private SmsCouponProductCategoryRelationService smsCouponProductCategoryRelationService;

    @Override

    public void create(SmsCouponDto smsCouponDto) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(smsCouponDto, smsCoupon);
        smsCouponMapper.insert(smsCoupon);
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponDto.getProductCategoryRelationList();
        List<SmsCouponProductRelation> productRelationList = smsCouponDto.getProductRelationList();
        for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
            smsCouponProductCategoryRelation.setCouponId(smsCoupon.getId());
        }
        for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
            smsCouponProductRelation.setCouponId(smsCoupon.getId());
        }
        smsCouponProductRelationService.saveBatch(productRelationList);
        smsCouponProductCategoryRelationService.saveBatch(productCategoryRelationList);
    }

    @Override
    public void deleteOne(Long id) {
        smsCouponMapper.deleteById(id);
        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsCouponProductRelation::getCouponId, id);
        smsCouponProductRelationMapper.delete(queryWrapper);
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SmsCouponProductCategoryRelation::getCouponId, id);
        smsCouponProductCategoryRelationMapper.delete(lambdaQueryWrapper);
    }

    @Override
    public CommonPage getAll(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<SmsCoupon> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != type, SmsCoupon::getType, type);
        queryWrapper.like(null != name, SmsCoupon::getName, name);
        Page<SmsCoupon> smsCouponPage = smsCouponMapper.selectPage(page, queryWrapper);
        CommonPage commonpage = CommonPage.getPage(smsCouponPage);
        return commonpage;
    }

    @Override
    public void updateOne(Long id, SmsCouponDto smsCouponDto) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtils.copyProperties(smsCouponDto, smsCoupon);
        smsCouponMapper.updateById(smsCoupon);
//        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(SmsCouponProductRelation::getCouponId, smsCoupon.getId());
//        List<SmsCouponProductRelation> smsCouponProductRelations = smsCouponProductRelationMapper.selectList(queryWrapper);
//        List<Long> pIds = CollUtil.getFieldValues(smsCouponProductRelations, "id", Long.class);
        smsCouponProductRelationService.updateBatchById(smsCouponDto.getProductRelationList());
        smsCouponProductCategoryRelationService.updateBatchById(smsCouponDto.getProductCategoryRelationList());
    }

    @Override
    public SmsCouponDto findOne(Long id) {
        SmsCoupon smsCoupon = smsCouponMapper.selectById(id);
        SmsCouponDto smsCouponDto = new SmsCouponDto();
        BeanUtils.copyProperties(smsCoupon, smsCouponDto);
        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsCouponProductRelation::getCouponId, id);
        List<SmsCouponProductRelation> smsCouponProductRelations = smsCouponProductRelationMapper.selectList(queryWrapper);
        smsCouponDto.setProductRelationList(smsCouponProductRelations);
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SmsCouponProductCategoryRelation::getCouponId, id);
        List<SmsCouponProductCategoryRelation> smsCouponProductCategoryRelations = smsCouponProductCategoryRelationMapper.selectList(lambdaQueryWrapper);
        smsCouponDto.setProductCategoryRelationList(smsCouponProductCategoryRelations);
        return smsCouponDto;
    }

}
